﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using MaxStudio.Concrete.DBUtility;

namespace MaxStudio.Concrete.Server.Dao
{
    public partial class ConcreteDaoDataContext : System.Data.Linq.DataContext
    {
        #region TransportDao

        public List<tb_Transport> GetAllTransport()
        {

            var query = (from obj in this.tb_Transports
                         where obj.IsDelete == 0
                         select obj).ToList();

            return query;

        }

        public List<tb_Transport> GetTransportByTransportType(int transportType)
        {

            var query = (from obj in this.tb_Transports
                         where obj.IsDelete == 0 && obj.TransportType==transportType
                         select obj).ToList();

            return query;

        }

        public List<tb_Transport> GetTransportByReferId(int referId)
        {

            var query = (from obj in this.tb_Transports
                         where obj.IsDelete == 0 && obj.ReferId == referId
                         select obj).ToList();

            return query; 
        }
        public List<tb_Transport> GetTransportByReferId(int referId, int transportType)
        {

            var query = (from obj in this.tb_Transports
                         where obj.IsDelete == 0 && obj.ReferId == referId && obj.TransportType == transportType
                         select obj).ToList();

            return query;
        }

        public tb_Transport GetTransportById(int id)
        {
            return this.tb_Transports.SingleOrDefault(tb_Transports => tb_Transports.Id == id);
        }

        public void CreateTransport(tb_Transport entity)
        {
            entity.CreateDate = DateTime.Now;
            entity.CreateBy = 1;
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            tb_Transports.InsertOnSubmit(entity);
            this.SubmitChanges();

        }

        public void ModifyTransport(tb_Transport entity)
        {
            tb_Transport instance = this.tb_Transports.SingleOrDefault(p => p.Id == entity.Id);
            instance.ReferId = entity.ReferId;
            instance.Count = entity.Count;
            instance.CountUnit = entity.CountUnit;
            instance.MotocadeId = entity.MotocadeId;
            instance.LoadingDate = entity.LoadingDate;
            instance.TransportType = entity.TransportType; 

            instance.IsDelete = entity.IsDelete;
            instance.ModifyBy = 1; //Need to get current user
            instance.ModifyDate = DateTime.Now;

            this.SubmitChanges();
        }
        public void DeleteTransport(tb_Transport entity)
        {
            tb_Transport deleteEntity = tb_Transports.SingleOrDefault(p => p.Id == entity.Id);
            tb_Transports.DeleteOnSubmit(deleteEntity);

            this.SubmitChanges();
        }

        #endregion

    }
}
